﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E.BLL;
using E.Model;
using E.Web.UI.Page;
using System.Text;
using X.Common;
namespace E.Web.Teacher.exam
{
    public partial class QuestionExport : TeacherBasePage
    {
        public string act;
        public string title;
        public int areaid, categoryid, courseid, isRequired;
        protected override void ShowPage()
        {
            act = XRequest.GetString("act");
            title = XRequest.GetString("TitleTxt");
            categoryid = XRequest.GetInt("CategorySlt", -1);
            courseid = XRequest.GetInt("CourseSlt", -1);
            isRequired = XRequest.GetInt("IsRequiredSlt", -1);

            IList<DictAreaModel> areaList = GetTeacherAreaList(teacherid);
            if (areaList.Count == 1)
            {
                areaid = areaList[0].ID;
            }
            else
            {
                areaid = XRequest.GetInt("AreaSlt", -1);
            }

            if (ispost)
            {
                QuestionBLL bll = new QuestionBLL();

                SearchQuestionModel searchModel = new SearchQuestionModel();
                searchModel.Title = title;
                searchModel.AreaID = areaid;
                searchModel.CategoryID = categoryid;
                searchModel.CourseID = courseid;
                searchModel.IsRequired = isRequired;
                searchModel.IsPublic = authority == E.Enum.EnumTeacherAuthority.Admin ? -1 : 1;

                IList<QuestionModel> list = bll.GetQuestionDetailInfoList(searchModel);
                if (list.Count > 0)
                {
                    try
                    {
                        StringBuilder sb = ExportTable(list);
                        ExportData(sb);
                    }
                    catch (Exception e)
                    {
                        System.Web.HttpContext.Current.Response.Write("数据读取错误");
                    }
                }
                else
                {
                    System.Web.HttpContext.Current.Response.Write("暂无数据");
                }
            }
        }


        private void ExportData(System.Text.StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            //System.Web.HttpContext.Current.Response.Charset = "GB2312";
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            //System.Web.HttpContext.Current.e = false;
            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
        private StringBuilder ExportTable(IList<QuestionModel> list)
        {
            StringBuilder sb = new StringBuilder("");
            sb.Append("<table border=\"1\" cellpadding=\"6\" cellspacing=\"2\" style=\"width:600px; font:12px '宋体'; border-collapse:collapse; border-color:#ccc; border-width:1px;\">\r\n");
            sb.Append("  <tr>\r\n");
            sb.Append("    <td rowspan=\"2\" style=\"font-weight:bolder;font:13px '宋体';\">课程名称</td>\r\n");
            sb.Append("    <td rowspan=\"2\" style=\"font-weight:bolder;font:13px '宋体';\">题目类型</td>\r\n");
            sb.Append("    <td rowspan=\"2\" style=\"font-weight:bolder;font:13px '宋体';\">题目名称</td>\r\n");
            sb.Append("    <td colspan=\"2\" style=\"font-weight:bolder;font:13px '宋体';\">选择项</td>\r\n");
            sb.Append("  </tr>\r\n");
            sb.Append("  <tr>\r\n");
            sb.Append("    <td style=\"font-weight:bolder;font:13px '宋体';\">项内容</td>\r\n");
            sb.Append("    <td style=\"font-weight:bolder;font:13px '宋体';\">正确项</td>\r\n");
            sb.Append("  </tr>\r\n");
            foreach (QuestionModel model in list)
            {
                int items = model.OptionList == null || (model.CategoryID == (int)Enum.EnumQuestionCategory.FITBs || model.CategoryID == (int)Enum.EnumQuestionCategory.SAQs) ? 0 : model.OptionList.Count;
                sb.Append("  <tr>\r\n");
                sb.AppendFormat("    <td rowspan=\"{0}\">{1}</td>\r\n", items, model.CourseName);
                sb.AppendFormat("    <td rowspan=\"{0}\">{1}</td>\r\n", items, model.CategoryName);
                sb.AppendFormat("    <td rowspan=\"{0}\">{1}</td>\r\n", items, model.Title);
                if (model.OptionList != null && items > 0 && (model.CategoryID == (int)Enum.EnumQuestionCategory.Choice || model.CategoryID == (int)Enum.EnumQuestionCategory.Judge))
                {
                    sb.AppendFormat("    <td >{0}</td>\r\n", model.OptionList.First().OptionTitle);
                    sb.AppendFormat("    <td >{0}</td>\r\n", model.OptionList.First().IsCorrect ? "√" : "");
                }
                else
                {
                    sb.AppendFormat("    <td >{0}</td>\r\n", "");
                    sb.AppendFormat("    <td >{0}</td>\r\n", "");
                }
                sb.Append("  </tr>\r\n");
                if (model.OptionList != null && items > 1 && (model.CategoryID == (int)Enum.EnumQuestionCategory.Choice || model.CategoryID == (int)Enum.EnumQuestionCategory.Judge))
                {
                    foreach (QuestionOptionModel option in model.OptionList.Skip(1))
                    {
                        sb.Append("  <tr>\r\n");
                        sb.AppendFormat("    <td >{0}</td>\r\n", option.OptionTitle);
                        sb.AppendFormat("    <td >{0}</td>\r\n", option.IsCorrect ? "√" : "");
                        sb.Append("  </tr>\r\n");
                    }
                }
            }
            sb.Append("</table>\r\n<p>&nbsp;</p>\r\n");
            Alert(sb.ToString());
            return sb;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

    }
}